//https://leetcode.cn/problems/number-of-provinces/submissions/582551024/
class Solution {
public:
    int findC(vector<int>& parent, int index) {
        if (parent[index] != index) {
            parent[index] = findC(parent, parent[index]);
        }
        return parent[index];
    }
    void unionC(vector<int>& parent, int index1, int index2) {
        parent[findC(parent, index1)] = findC(parent, index2);
    }
    int findCircleNum(vector<vector<int>>& isConnected) {
        int len = isConnected.size();
        vector<int> parent(len);
        for (int i = 0; i < len; i++) {
            parent[i] = i;
        }
        for (int i = 0; i < len; i++) {
            for (int j = i + 1; j < len; j++) {
                if (isConnected[i][j] == 1) {
                    unionC(parent, i, j);
                }
            }
        }
        int re = 0;
        for (int i = 0; i < len; i++) {
            if (parent[i] == i) {
                ++re;
            }
        }
        return re;
    }
};